

package com.ims.common.dao.login;

import java.util.List;

import com.ims.common.dao.BaseDAOHibImpl;
import com.ims.stocks.model.Employee;
import com.ims.stocks.model.Role;
import com.ims.stocks.model.UserDetails;




public class LoginDaoImpl extends BaseDAOHibImpl implements LoginDao {


    @Override
    public String getPassword(String userid) {
	return null;
    }


    @Override
    public UserDetails getUserDetails(String loginId) {
	UserDetails userDetails = null;

	List<UserDetails> userDetailsLst = getHibernateTemplate().find(
			"from UserDetails where LOWER(loginId)=? ", loginId.toLowerCase()  );

	if(!userDetailsLst.isEmpty()) {
	    userDetails = userDetailsLst.get(0);
	}
	
	return userDetails;

    }
    public UserDetails getUserDetails(String username, String password){
    	UserDetails userDetails = null;

    	List<UserDetails> userDetailsLst = getHibernateTemplate().find(
    			"from UserDetails where LOWER(loginId)=? and password=? ",new Object[]{ username.toLowerCase(),password}  );

    	if(!userDetailsLst.isEmpty()) {
    	    userDetails = userDetailsLst.get(0);
    	}
    	
    	return userDetails;

    }
   
    @Override
    public Employee getUserInfo(String emailId) {
	int slNo=0;
	getHibernateTemplate().setCacheQueries(true);
	List<Employee> employee = (List<Employee>) getHibernateTemplate().find(
			"from Employee where LOWER(emailId)=? ", emailId.toLowerCase());
	if(employee.isEmpty()){
	    return null;
	}else{
	    return employee.get(0);
	}
	    
	
    }


    @Override
    public void updateLastAccessed(UserDetails userDetails) {
	
	getHibernateTemplate().merge(userDetails);
    }



    public List<Role> getEmpRole(Integer id) {
	      String hql = "from Role rp where rp.status = 1 AND rp.employee in (from Employee e where e.slNo =?)";
	      Object[] arr = new Object[] { id };
	      List<Role> result = find(hql, arr);
	      return result;
	    }

  

   

}
